#! /bin/bash

source /etc/profile

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
echo "unline_path="${unline_path}
echo "params="$1
if [[ ! -z $1 ]] && [[ -d $1 ]] && [[ -z ${unline_path} ]]
then
PREHOME=$1
elif [[ ! -z ${unline_path} ]]
then
PREHOME=${unline_path}
else
PREHOME=${HOME}"/unline"
fi
DIR=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
#当前脚本上一层路径
PARENTPATH=$(dirname $(dirname ${DIR}))
SOURCEFILE=`sh ${PARENTPATH}/getunlinefile.sh ${PREHOME} finalshell`
install_path=""
package_download_url=http://dl.hostbuf.com/finalshell2/finalshell_linux.zip?v=$RANDOM
package_save_name=finalshell_data_linux.zip
if [[ ! -z ${SOURCEFILE} ]]
then
  echo "set whole unline"
  package_save_name="${PREHOME}/finalshell/${SOURCEFILE}"
fi
echo "package_save_name="${package_save_name}
function checkOS_TYPE(){
    if [[ ! -z "`uname | grep Darwin`" ]];then
        OS_TYPE=osx
		install_path=/Applications
    elif [[ ! -z "`uname | grep Linux`" ]];then
        OS_TYPE=linux
		install_path=/usr/lib
    else
        echo "Unsupported operating systems!"
        exit 1
    fi
}

function checkos(){
    if [[ -f /etc/redhat-release ]];then
        OS=centos
    elif [[ ! -z "`cat /etc/issue | grep bian`" ]];then
        OS=debian
    elif [[ ! -z "`cat /etc/issue | grep Ubuntu`" ]];then
        OS=ubuntu
    else
        echo "unknow"
    fi
	echo $OS
}

function install(){
   echo  "${EUID}  正在安装FinalShell...$package_save_name"
   if [ $EUID != 0 ]; then
    sudo sh "$0" "$unline_path"
    exit $?
   fi
   echo 2222222222222${package_save_name}
   # rm -f $package_save_name
   if [[ ! -f ${package_save_name} ]]
   then
   if [[ $OS_TYPE = "osx" ]]; then
   n=0
   until [ $n -ge 10 ]
   do
      echo "Downloading finalshell_data.zip..."
      curl -L -o $package_save_name $package_download_url && break
      n=$[$n+1]
      sleep 2
   done
    elif [[ $OS_TYPE = "linux" ]]; then
		if ! wget -O $package_save_name $package_download_url ; then
			echo "Download software failed!"
			exit 1
		else
			echo "Download complete!"
		fi
    else
        echo "Unsupported operating systems!"
        exit 1
    fi
  fi
	unzip -o $package_save_name  -d $install_path

	cd $install_path/FinalShell/lib/app/

	#sudo chmod 775 ./unpack200;
	#./unpack200 finalshell.pack.gz finalshell.jar
	#rm -f finalshell.pack.gz
	#./unpack200 lib-run/bcprov-jdk15on-160.pack.gz lib-run/bcprov-jdk15on-160.jar
	#rm -f lib-run/bcprov-jdk15on-160.pack.gz

	\cp img/finalshell.png /usr/share/icons/hicolor/scalable/apps
	\cp finalshell.desktop /usr/share/applications
	chmod +x /usr/share/applications/finalshell.desktop

	sudo find /usr/lib/FinalShell -type f -exec chmod 644 {} \;
	sudo find //usr/lib/FinalShell -type d -exec chmod 755 {} \;

	sudo chmod -R 755 /usr/lib/FinalShell/lib/runtime/lib/classlist;
	sudo chmod -R 755 /usr/lib/FinalShell/lib/runtime/lib/jexec;
	sudo chmod -R 755 /usr/lib/FinalShell/lib/runtime/lib/jspawnhelper;
	sudo chmod -R 755 /usr/lib/FinalShell/lib/runtime/lib/modules;
	sudo chmod -R 755 /usr/lib/FinalShell/lib/runtime/lib/security/cacerts;

	sudo chmod 755 /usr/lib/FinalShell/bin/FinalShell;

	#sudo chmod 755 /usr/lib/FinalShell/app/unpack200;
	#sudoer=$(who am i | awk '{print $1}');
	sudoer=$USER
	sudo chown -R $sudoer /usr/lib/FinalShell
	#sudo chown -R $USER:$USER /home/$USER/fsdownload/
	sudo setcap cap_net_raw,cap_net_admin=eip /usr/lib/FinalShell/bin/FinalShell
	#/usr/lib/FinalShell/FinalShell
	echo  "  FinalShell安装完成"

}

checkOS_TYPE
checkos
echo startinstall
install
cp -rf ${PREHOME}/finalshell/backup/* ${HOME}/.finalshell/
